﻿namespace ManagedChangeJournal
{
    using System;
    using System.IO;
    using System.Collections.Generic;

    public static class VolumeHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string[] GetValidVolumes()
        {
            List<String> volumes = new List<String>();
            DriveInfo[] infos = DriveInfo.GetDrives();

            foreach (DriveInfo info in infos)
            {
                if (info.IsReady)
                {
                    if (info.DriveFormat == "NTFS")
                    {
                        if (info.Name != String.Empty)
                            volumes.Add(info.Name);
                        else if (info.VolumeLabel != String.Empty)
                            volumes.Add(info.VolumeLabel);
                    }
                }
            }

            return volumes.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string[] GetValidVolumePaths()
        {
            string[] volumes = GetValidVolumes();
            List<string> paths = new List<string>();

            foreach (String volume in volumes)
            {
                paths.Add(GetValidVolumePath(volume));
            }

            return paths.ToArray();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="driveNameOrLabel"></param>
        /// <returns></returns>
        public static string GetValidVolumePath(string driveNameOrLabel)
        {
            string temp = driveNameOrLabel;
            temp = temp.Replace(":", "");
            temp = temp.Replace(Path.DirectorySeparatorChar.ToString(), "");
            return string.Format("{0}{0}.{0}{1}:",
                Path.DirectorySeparatorChar, temp);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="formattedPath"></param>
        /// <returns></returns>
        public static bool VolumePathIsValid(string formattedPath)
        {
            if (formattedPath.StartsWith("\\\\.\\") &&
                formattedPath.EndsWith(":"))
                return true;
            return false;
        }
    }
}
